// pages/tripDetail/tripDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [{
                "text": "日程",
                "iconPath": "/images/tabbaricon/日历-0.png",
                "selectedIconPath": "/images/tabbaricon/日历-1.png",
            },
            {
                "text": "记账",
                "iconPath": "/images/tabbaricon/calc_2.png",
                "selectedIconPath": "/images/tabbaricon/calc_1.png"
            },
            {
                "text": "清单",
                "iconPath": "/images/tabbaricon/check-0.png",
                "selectedIconPath": "/images/tabbaricon/check-1.png"
            }
        ],
        currentPage: 0,
        tripId: '',
        isChangeTitle: false,
        newTitle: "",
        //
        deleteTempDayId: {},
        deleteAccId: "",
        showActionsheet_deleteDay: false,
        showActionsheet_more: false,
        showActionsheet_deleteTrip: false,
        showActionsheet_deleteAcc: false,
        groups_deleteDay: [{
            text: '确认删除',
            type: 'warn',
            value: 1
        }],
        groups_deleteTrip: [{
            text: '确认删除',
            type: 'warn',
            value: 2
        }],
        groups_more: [{
                text: '修改行程标题',
                value: 0
            },
            {
                text: '删除行程',
                type: 'warn',
                value: 1
            }
        ],
        //
        begindate: '',
        enddate: '',
        accountingData: {},
        classifiIcon:[
            "/images/classifi/classifi0.png",
            "/images/classifi/classifi1.png",
            "/images/classifi/classifi2.png",
            "/images/classifi/classifi3.png",
            "/images/classifi/classifi4.png",
            "/images/classifi/classifi5.png",
            "/images/classifi/classifi6.png"],
        checkListData: []
    },

    getCheckList:function() {
        var _this = this
        wx.cloud.callFunction({
            name:'getCheckList',
            tripid: _this.data.tripId
        }).then(res => {
            console.log(res.result)
            _this.setData({
                checkListData: res.result
            })
            wx.hideLoading({
                success: (res) => {
                    console.log("hideLoading")
                },
              })
        })
    },
    changeChecked:function(e) {
        wx.showLoading()
        var _this = this
        console.log(e.currentTarget.dataset.checkid)
        wx.cloud.callFunction({
            name:'changeChecked',
            data: {
                checkid: e.currentTarget.dataset.checkid,
                tripid: _this.data.tripId
            }
        }).then(res => {
            _this.getCheckList()
        })
    },

    tabChange(e) {
        console.log('tab change', e.detail.index)
        this.setData({
            currentPage: e.detail.index
        })

    },
    //新增一天
    addEmptyDay: function () {
        wx.showLoading({
            title: '添加中',
        })
        var that = this
        console.log("addEmptyDay")
        wx.cloud.callFunction({
            name: 'addTripDay',
            data: {
                tripId: that.data.tripId
            }
        }).then(res => {
            console.log("successAddDay")
            that.getTripInfo(that.data.tripId, 1)
        })
    },
    //显示更多AS
    showAS_More: function (e) {
        this.setData({
            showActionsheet_more: true,
            showActionsheet_deleteDay: false,
            showActionsheet_deleteTrip: false,
        })
    },

    btnClickASMore: function (e) {
        console.log(e.detail.index)
        this.closeASMore()
        if (e.detail.index == 1) {
            //deleteTrip
            this.setData({
                showActionsheet_deleteTrip: true
            })
        } else if (e.detail.index == 0) {
            console.log("修改triptitle")
            this.setData({
                isChangeTitle: true
            })

        } else if (e.detail.index == 1) {
            console.log("修改TripTime")
        }
    },

    btnClickASTrip: function (e) {
        wx.showLoading({
            title: '删除中',
        })
        var that = this
        //
        wx.cloud.callFunction({
            name: 'deleteTrip',
            data: {
                tripId: that.data.tripId
            }
        }).then(res => {
            wx.showToast({
                title: '删除成功',
                duration: 2000,
                success: function () {
                    setTimeout(function () {
                        wx.navigateBack()
                    }, 2000)
                }
            })
        }).catch(err => {
            wx.showToast({
                title: '删除失败',
                icon: 'error'
            })
        })
    },

    closeASMore: function () {
        this.setData({
            showActionsheet_more: false
        })
        console.log("close AS More")
    },

    closeASTrip: function () {
        this.setData({
            showActionsheet_deleteTrip: false
        })
        console.log("close AS Trip")
    },

    closeASAcc: function () {
        this.setData({
            showActionsheet_deleteAcc: false
        })
    },

    //移除一天AS
    deleteDay: function (e) {
        this.setData({
            showActionsheet_deleteDay: true,
            showActionsheet_deleteTrip: false,
            showActionsheet_more: false,
            deleteTempDayId: e.currentTarget.dataset
        })
    },

    closeASDay: function () {
        this.setData({
            showActionsheet_deleteDay: false,
            deleteTempDayId: {}
        })
        console.log("close AS Delete Day")
    },
    btnClickASDay(e) {
        var that = this
        wx.showLoading({
            title: '正在删除',
        })
        console.log(e)
        console.log("deleteDay", this.data.deleteTempDayId)
        wx.cloud.callFunction({
            name: 'deleteTripDay',
            data: {
                dayId: that.data.deleteTempDayId.id,
                dayIndex: that.data.deleteTempDayId.dayindex,
                tripId: that.data.tripId
            }
        }).then(res => {
            console.log(res)
            that.getTripInfo(that.data.tripId, 2)
        })
        this.closeASDay()
    },
    //

    getTripInfo: function (id, type) {
        var that = this
        console.log(id)
        this.setData({
            tripId: id
        })
        wx.cloud.callFunction({
            name: 'getMyTripDetail',
            data: {
                tripId: id
            }
        }).then(res => {
            if (type == 0) {
                wx.hideLoading({
                    success: (res) => {},
                })
            } else if (type == 1) {
                wx.showToast({
                    title: '新增成功',
                    icon: 'success'
                })
            } else if (type == 2) {
                wx.showToast({
                    title: '已删除',
                    icon: 'success'
                })
            }
            console.log(res.result)
            that.setData({
                daysInfo: res.result,
                isChangeTitle: false
            })
        })
    },

    //修改标题

    getNewTitle: function (e) {
        console.log(e.detail.value)
        this.setData({
            newTitle: e.detail.value
        })
    },

    submitNewTitle: function () {
        var that = this
        if (this.data.newTitle != "") {
            wx.showLoading({
                title: '正在修改',
            })
            console.log("submitNewTitle" + this.data.newTitle)
            wx.cloud.callFunction({
                name: 'changeTrip',
                data: {
                    tripId: that.data.tripId,
                    newName: that.data.newTitle
                }
            }).then(res => {
                wx.showToast({
                    title: '修改成功',
                    icon: 'success'
                })
                that.getTripInfo(that.data.tripId, 0)
            })
        } else {
            this.setData({
                isChangeTitle: false
            })
            console.log("无修改")
        }
    },

    //修改时间

    getToday: function () {
        var now = new Date()
        var year = now.getFullYear(); //得到年份
        var month = now.getMonth(); //得到月份
        var date = now.getDate(); //得到日期
        month = month + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        var todaytime = "";
        todaytime = year + "-" + month + "-" + date
        return {
            beginTime: year - 1 + "-" + month + "-" + date,
            endTime: year + 1 + "-" + month + "-" + date
        }
    },

    bindBeginDateChange: function (e) {
        var that = this
        console.log(e.detail.value)
        wx.showLoading({
            title: '正在修改'
        })
        wx.cloud.callFunction({
            name: 'changeTrip',
            data: {
                newBeginDate: Date.parse(e.detail.value),
                tripId: that.data.tripId
            }
        }).then(res => {
            that.getTripInfo(that.data.tripId, 0)
            wx.showToast({
                title: '修改成功',
                icon: 'success'
            })
        })
    },

    naviToDay: function (e) {
        var that = this
        console.log(e.currentTarget.dataset.dayindex)
        wx.navigateTo({
            url: '/pages/dayDetail/dayDetail?index=' + e.currentTarget.dataset.dayindex + "&id=" + e.currentTarget.dataset.id + "&time=" + that.data.daysInfo.tripBeginTime,
        })
    },


    addAccount: function (e) {
        console.log(e.currentTarget.dataset.tripid)
        wx.navigateTo({
            url: '/pages/addAccount/addAccount?tripId=' + e.currentTarget.dataset.tripid,
        })
    },
    deleteAcc: function (e) {
        console.log(e.currentTarget.dataset.accid)
        this.setData({
            deleteAccId: e.currentTarget.dataset.accid,
            showActionsheet_deleteAcc: true
        })
    },

    btnClickASAcc: function(e) {
        var _this = this
        wx.showLoading({
            title: '正在删除',
        })
        console.log(e)
        wx.cloud.callFunction({
            name: 'deleteaccountRecord',
            data: {
                accid: _this.data.deleteAccId
            }
        }).then(res => {
            _this.onShow()
            wx.showToast({
                title: '删除成功',
                duration: 2000,
                success: function () {
                    setTimeout(function () {
                        wx.navigateBack()
                    }, 2000)
                }
            })
        }).catch(err => {
            wx.showToast({
                title: '删除失败',
                icon: 'error'
            })
        })
        this.closeASAcc()
    },


    getAccountData: function () {
        var _this = this
        wx.cloud.callFunction({
            name:'getAccountRecord',
            data: {
                tripid: _this.data.tripId
            }
        }).then(res => {
            console.log(res.result)
            _this.setData({
                accountingData: res.result
            })
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        wx.showLoading({
            title: '加载中',
        })
        this.getTripInfo(e.id, 0)
        this.setData({
            tripId:e.id
        })
        this.setData({
            begindate: this.getToday().beginTime,
            enddate: this.getToday().endTime
        })
        this.getCheckList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAccountData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})